<template>
  <div class="other">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>信息记录</span>
        </div>
      </template>
      <el-button type="primary" class='btn' @click="add">新增</el-button>
      <el-table :data="tableData" style="width: 100%; margin-top: 20px" :row-class-name="tableRowClassName"
        class="my-table">
        <el-table-column prop="" label="序号" width="80">
          <template #default="scope">
            <el-tag type="info">{{ scope.$index + 1 }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="名称" width="200"/>
        <el-table-column label="链接地址">
          <template #default="scope">
            <a :href="scope.row.link" target="_blank" rel="noopener noreferrer">
              {{ scope.row.link }}
            </a>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" width="350"/>
        <el-table-column prop="author" label="联系人" />
        <el-table-column label="操作" width="160">
          <template #default="scope">
            <el-button size="small" @click="editOrder(scope.row.id)" v-show="username == scope.row.author">编辑</el-button>
            <el-button type="danger" size="small" @click="deleteOrder(scope.row.id)"
              v-show="username == scope.row.author">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted, nextTick } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import workApi from '../../api/work'
import { useRouter } from 'vue-router'
const router = useRouter()

const tableData = ref([])
const username = sessionStorage.getItem('username') //日志需要
// 获取全局变量
const baseURL = window.globalConfig.baseURL

// 搜索条件
const searchForm = reactive({
  current: 1,
  size: 20,
  name: '',
  year: '',
})

onMounted(async () => {
  //请求数据
  getOthers()
})

// 获取数据列表
const getOthers = async () => {
  const res = await workApi.getOthers();
  console.log(res)
  tableData.value = Object.assign([], res.data);
}


const add = async () => {
  router.push({
    path: '/other/add',
  })
}

const editOrder = async (id) => {
  router.push({
    path: '/other/add',
    query: { id: id },
  })
}

// 删除项目
const deleteOrder = (id) => {
  ElMessageBox.confirm('确定要删除该项目吗?', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(async () => {
      const username = sessionStorage.getItem('username')
      const res = await workApi.delOther({ id: id, username: username })
      if (res.data.success) {
        ElMessage.success('删除成功')
        getOthers()
      } else {
        ElMessage.error('删除失败')
      }
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消删除',
      })
    })
}

</script>

<style scoped>
.el-table .warning-row {
  --el-table-tr-bg-color: var(--el-color-warning-light-9);
}

.el-table .success-row {
  --el-table-tr-bg-color: var(--el-color-success-light-9);
}

.el-tag.ml-2 {
  margin: 4px;
  cursor: pointer;
}

.el-table thead tr {
  font-size: 17px;
}

.el-progress-circle {
  width: 50px !important;
  height: 50px !important;
}

.el-progress__text span {
  font-size: 13px;
}

.el-table {
  --el-table-index: none;
}

.el-dialog__header {
  margin-right: 0;
  padding: 0;
}
.other .btn {
  float: right;
  margin-bottom: 20px;
  margin-right: 10px
}
</style>
